using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using CS6238Project2.Banking.Communication;
using CS6238Project2.Banking.Commerce;
using CS6238Project2.Banking.Tools;
using System.Net.Security;
using System.Security.Cryptography;  

namespace CS6238Project2.Banking.Tools
{
    class socket
    {
        //private byte[] m_DataBuffer = new byte[4096];
        //IAsyncResult m_asynResult;
        //AsyncCallback pfnCallBack;
        //private Socket m_socClient;
        //private Parcel receData;
        // create the socket...
        public socket(string IPaddr, int port, packet p)
        {
            Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // get the remote IP address...
            IPAddress ip = IPAddress.Parse(IPaddr);
            //int iPortNo = System.Convert.ToInt32(port, 10);
            ;
            //create the end point
            IPEndPoint ipEnd = new IPEndPoint(ip, port);
            //connect to the remote host...
            m_socClient.Connect(ipEnd);
            IPAddress[] a = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    data.SenderAddr = a[i].ToString();
                }
            }
            

            string xmlString = Encryption.XmlString(p);


            //Now turn the string into a byte array
            m_socClient.Send(Encryption.StringToUTF8ByteArray(xmlString));
           
        }
    }
}
